<?php
/***************************************************************************
File Name 	: index.php
----------------------------------------------------------------------------
Author		: David W. Pichard, II
Copyright	: (C) 2004 Pichard Design
Email		: info@picharddesign.com
URL  		: http://www.picharddesign.com/
Description	: Displays the entire page for the Traffic System.
Date Created: Monday  April 18, 2004 06:05:41
****************************************************************************/
session_start();
ob_start("ob_gzhandler"); 
include_once('stationInfo.php');
include_once('config.php');
require_once 'DB.php';
$db = DB::connect($dsn);
if (DB::isError($db)) {
die ($db->getMessage());
}

 
// Writes main page
if(isset($_GET['ID'])){
	$ID = $_GET['ID'];
}else{
	$ID = 0;
}


$i = 0;
while($i < count($menuItems)){
$menuItemsID[$i] = $i;
++$i;
}
$i = ""; 

//initialize Vars
if(isset($_GET['subMenu'])){
$subMenu = $_GET['subMenu'];
}else{
$subMenu = "2";
}

if(isset($_GET['action'])){
$action = $_GET['action'];
}else{
$action = "";
}

echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n
<HTML>
<HEAD>";

echo("<TITLE>$pageTitle</TITLE>\n");

echo"<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n
<META http-equiv=\"Content-Language\" content=\"EN-US\">\n
<META name=\"author\" content=\"PichardDesign.com\">\n
<META name=\"distribution\" content=\"Global\">\n
<META name=\"copyright\" content=\"Copyright 2004, PichardDesign\">\n
<META name=\"robots\" content=\"FOLLOW,INDEX\">\n
<LINK href=\"style.css\" rel=\"stylesheet\" type=\"text/css\">\n";

		//Swallow enter code for IE or netscape
			if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE")) {
					echo("<script language=\"JavaScript\" TYPE=\"text/javascript\">\n
					browser = \"IE\";
					</script>\n");
					$browser = 'IE'; 
			}else{
 			echo("<script language=\"JavaScript\"  TYPE=\"text/javascript\">\n
			browser = \"other\";
			</script>\n\n");  
			}
			
echo"<script src=\"includes/js/functions.js\" type=\"text/javascript\" language=\"javascript\"></script>\n";



//script for Quick Add
if($ID == '1' && $subMenu == '1'){
echo"<script src=\"includes/js/quickAdd.js\" type=\"text/javascript\" language=\"javascript\"></script>\n";
	
	
}

echo"</HEAD>\n";



if($subMenu == '0' && $ID == '0' && ($action == 'enter' || $action == 'search')){
	echo("<body BGCOLOR=\"$BGColor\" onLoad=\"document.form.name.focus()\">");
}else if($subMenu == '2' && $ID == '0' && $action == 'enter'){
	echo("<body BGCOLOR=\"$BGColor\" onLoad=\"document.form.name.focus()\">");
}else if($subMenu == '1' && $ID == '0' && $action == 'enter'){
	echo("<body BGCOLOR=\"$BGColor\" onLoad=\"document.form.hour.focus()\">");
}else if($subMenu == '3' && $ID == '0' && $action == 'enter'){
	echo("<body BGCOLOR=\"$BGColor\" onLoad=\"document.form.programName.focus()\">");
}else if($ID == '0' && $subMenu == '2' && ($action == 'planner' || !$action)){
	echo("<body BGCOLOR=\"$BGColor\">\n");
	echo "<script src=\"includes/js/dragLayer.js\" type=\"text/javascript\" language=\"JavaScript1.2\"></script>\n";
//Hidden Div used to display popup window on Grid PLanner....
include ('includes/grid/WeeklyPlannerPopup.php');	
}else if($ID == '1' && $subMenu == '0' && $action == 'enterPrograms'){//set focus for entering new Programs in Daily Log Section
	echo("<body BGCOLOR=\"$BGColor\" onLoad=\"document.form.title.focus()\">");
}else if($ID == '1' && $subMenu == '0' && $action == 'searchPrograms'){//set focus for entering new Programs in Daily Log Section
	echo("<body BGCOLOR=\"$BGColor\" onLoad=\"document.form.name.focus()\">");
}else if($ID == '1' && $subMenu == '1'){//Load javascript for View Daily Log
	echo("<body BGCOLOR=\"$BGColor\">");
	echo "<script src=\"includes/js/dragLayer.js\" type=\"text/javascript\" language=\"JavaScript1.2\"> </script>\n";
}else{
echo("<body BGCOLOR=\"$BGColor\">");
}






//SCRIPT FOR MAIN MENU
echo"<script type='text/javascript'>

//HV Menu v5.411- by Ger Versluis (http://www.burmees.nl/)
//Submitted to Dynamic Drive (http://www.dynamicdrive.com)
//Visit http://www.dynamicdrive.com for this script and more

function Go(){return}

</script>
<script type='text/javascript' src='includes/js/main_menu.js'></script>
<script type='text/javascript' src='includes/js/menu_com.js'></script>
<noscript>Your browser does not support script</noscript>
";



echo "<CENTER>
<TABLE bgcolor=\"#ffffff\" width=\"750\" border=\"2\" cellspacing=\"0\" cellpadding=\"0\">
  <TR>
    <TD align=\"center\" style=\"vertical-align: top;\">
<!--Begin Main Header Table-->
<TABLE width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\">
  <TR bgcolor=\"#CCCCCC\">
    <TD height=\"20\" align=\"right\"  style=\"vertical-align: middle; padding-right:5px;\" bgcolor=\"$colorTheme\"> <FONT color=\"$activeSectionFont\" size=\"-1\"><B>";
	
	//Display Current date at top Right of Table
	$myDate = date("l- M. d, Y");
	echo("$myDate");
	
	echo"</B></FONT>
	</TD>
  </TR>
  <TR bgcolor=\"#CCCCCC\">
    <TD height=\"25\" bgcolor=\"$colorTheme\" align=\"left\" style=\"padding-left:4px;\">
	<STRONG><FONT color=\"$activeSectionFont\">Messages:</FONT>&nbsp;</STRONG>";

		if(isset($_SESSION['message'])){
			$message = $_SESSION['message'];
		}else if(isset($_POST['message'])){
			$message = $_POST['message'];
		}else{
			$message = "There are no current messages.";
		}

		
		
		
	echo("<font color=\"$highLightFont\" size=\"-1\"><B>$message</B></font>");
	
	
	echo"</TD>
  </TR>
</TABLE>
<!--End Main Header Table-->
<!--Begin Content and Menu Table-->
<TABLE width=\"750\"   border=\"1\" cellpadding=\"0\" cellspacing=\"0\" >
 <TR bgcolor=\"#666666\">
    <TD bgcolor=\"$colorTheme\" width=\"115\" style=\"vertical-align: top;\" align=\"left\" >
	
	<!--Begin Menu Cell-->
	<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"115\" >
	<TR>
		<TD align=\"center\" class=\"mainMenuHeaderColor\" nowrap>Main Menu</TD>
	</TR>";
	
	//###############################################
	//BEGIN MAIN MENU CELL ON LEFT RAIL
	//###############################################
		echo "<TR><TD><div id='MenuPos' style='position:relative; width:112px; height:127px;'></div></TD></TR> ";

	echo "</TABLE>";

	//###############################################
	//END MAIN MENU CELL ON LEFT RAIL
	//###############################################	
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>	
	//###############################################
	//BEGIN LIST PREVIOUS SPOTS FOR GRID ENTRY FORM
	//###############################################
	if($ID == '0' && $subMenu == '2' && $action == 'enter' && isset($_SESSION['viewSpots'])){
		echo"<script language=\"JavaScript\" type=\"text/javascript\">
		<!--
		function changeSpotID(mySpot){
		document.form.spotID.value = mySpot;
		}
		//-->
		</script>";
		if(count($_SESSION['viewSpots']) > 0){
				echo"<P><TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"115\" style=\"background-color:#000000;\">";
				echo"<TR><TD class=\"mainMenuHeaderColor\">Current Spots</TD></TR>";
					for ($i = 0; $i < count($_SESSION['viewSpots']); $i++) {
						echo"<TR><TD align=\"left\" class=\"switchboardInfo\">&#8226; <A HREF=\"javascript:changeSpotID('" . current($_SESSION['viewSpots']) . "')\">" . current($_SESSION['viewSpots']) . "</TD></TR>";
						next($_SESSION['viewSpots']);
					}
				echo"</TABLE></P>";
		}
	}
	
	//###############################################
	//END LIST PREVIOUS SPOTS FOR GRID ENTRY FORM
	//###############################################	
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	if(isset($_GET['TS'])){
		$_SESSION['timestamp'] = $_GET['TS'];
	}	
	

	
			if(!$_SESSION['timestamp']){
					//Function to find nearest Monday for current week
					function findNearestMonday($day,$startDay,$startMonth,$startYear){
					if($day == '0'){
					$monday = ($startDay - 6);
						if($monday < '0' && $startMonth != '1'){
						$lastMonthsmonday = (date("t",mktime(0,0,0,($startMonth - 1),$startDay,$startYear)) + $monday);
						$_SESSION['timestamp'] = mktime(0,0,0,($startMonth - 1),$lastMonthsmonday,$startYear);
						//January - go to December for nearest monday
						}else if($monday < '0' && $startMonth == '1'){
						$lastMonthsmonday = (date("t",mktime(0,0,0,12,$startDay,($startYear - 1))) + $monday);
						$_SESSION['timestamp'] = mktime(0,0,0,12,$lastMonthsmonday,($startYear - 1));
						}else{
						$_SESSION['timestamp'] = mktime(0,0,0,$startMonth,$monday,$startYear);
						}
					
					}else{
					$monday = ($startDay - $day) + 1;
					$_SESSION['timestamp'] = mktime(0,0,0,$startMonth,$monday,$startYear);
					}
					

				}
				findNearestMonday(date("w", mktime(0,0,0,date("n"),date("j"),date("Y"))),date("j"),date("n"),date("Y"));
				//end function
				
			}// end if(!$_SESSION['timestamp']){
			
			
			
			
		
	if($ID < '1' && ($subMenu == 2 || !isset($subMenu)) && $action != "enter" && $action != "view" && $action != "verify" && $action != "modifyGrid"){
	echo "<FORM name=\"pdf\" action=\"pdf/generatepdf.php\" method=\"get\">";
	echo "<input name=\"TS\" type=\"hidden\" value=\"".$_SESSION['timestamp']."\"><P align=\"center\"><INPUT TYPE=\"image\" SRC=\"images/Print_Grid.gif\" style=\"border:0px; padding-bottom:5px;\" ALT=\"Print This Grid View\"></P>";
	echo"</FORM>";
	echo "<P></P>";
	//###################################################################################
	//BEGIN MENU FOR SWITCH BOARD
	//###################################################################################	

	echo "<div id=\"glideDiv\"><TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" style=\"background-color:#000000;\">
	<form action=\"formaction.php\" method=\"post\" name=\"updates\" id=\"switchBoardUpdates\">
	<TR><TD id=\"hiddenStuff\"></TD></TR>
	
	<TR><TD align=\"center\" class=\"mainMenuHeaderColor\" nowrap>Moves</TD></TR>
	<TR><TD align=\"center\"  class=\"switchboardInfo\" nowrap id=\"Moves\"></TD></TR>
	
	<TR><TD align=\"center\" class=\"mainMenuHeaderColor\" nowrap>Copies</TD></TR>
	<TR><TD align=\"center\"  class=\"switchboardInfo\" nowrap id=\"Copies\" ></TD></TR>
	
	<TR><TD align=\"center\" class=\"mainMenuHeaderColor\" nowrap>Updates</TD></TR>
	<TR><TD align=\"center\"  class=\"switchboardInfo\" nowrap id=\"UpdatesYo\" ></TD></TR>
	
	<TR><TD align=\"center\" class=\"mainMenuHeaderColor\" nowrap>Deletes</TD></TR>
	<TR><TD align=\"center\"  class=\"switchboardInfo\" nowrap id=\"Deletes\" ></TD></TR>
	
	<TR><TD align=\"center\" style=\"background-color:#666666; padding-top:5px; padding-bottom:3px;\"><input name=\"action\" type=\"hidden\" value=\"switchBoard\"><input name=\"submitSwitchBoard\" type=\"button\" class=\"inputButton\"  value=\"Submit\" onClick=\"submitSwitchboard()\"></TD></TR>
	</form>
	</TABLE></div>";
	
	
	//###################################################################################
	//END MENU FOR SWITCH BOARD
	//###################################################################################		
	
	}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	echo "<P></P>";
	//###################################################################################
	//BEGIN OPTIONS TABLE FOR LIVE GRID
	//###################################################################################
	if($ID == '0' && $subMenu == '2' && $action == 'view'){
	
			//+++++++++++++++++++++++++++++++++++++		
			//BEGIN sort sort method for live grid		
			if(!isset($_GET['sortMethod']) && !isset($_SESSION['sortMethod'])){
				$sortMethod = 'time';
			}else if(isset($_GET['sortMethod'])){
				$sortMethod = $_GET['sortMethod'];
				$_SESSION['sortMethod'] = $_GET['sortMethod'];
			}else{
				$sortMethod = $_SESSION['sortMethod'];
			}			
			//END sort sort method for live grid		
			//+++++++++++++++++++++++++++++++++++
	
		echo "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" style=\"background-color:#000000;\">
		<TR><TD align=\"center\" class=\"mainMenuHeaderColor\">Sort By:</TD>	</TR>";
		
		if($sortMethod == 'time'){
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" style=\"color:#000000;\" nowrap>Time &raquo;</TD></TR>");
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" nowrap><A HREF=\"index.php?ID=0&amp;subMenu=2&amp;action=view&amp;TS=".$_SESSION['timestamp']."&amp;sortMethod=client\">Client Name</A></TD></TR>");
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" nowrap><A HREF=\"index.php?ID=0&amp;subMenu=2&amp;action=view&amp;TS=".$_SESSION['timestamp']."&amp;sortMethod=allSpots\">Spot</A></TD></TR>");
		}else if($sortMethod == 'client'){
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" nowrap><A HREF=\"index.php?ID=0&amp;subMenu=2&amp;action=view&amp;TS=".$_SESSION['timestamp']."&amp;sortMethod=time\">Time</A></TD></TR>");
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" style=\"color:#000000;\" nowrap>Client Name &raquo;</TD></TR>");
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" nowrap><A HREF=\"index.php?ID=0&amp;subMenu=2&amp;action=view&amp;TS=".$_SESSION['timestamp']."&amp;sortMethod=allSpots\">Spot</A></TD></TR>");
		}else if ($sortMethod == 'allSpots'){
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" nowrap><A HREF=\"index.php?ID=0&amp;subMenu=2&amp;action=view&amp;TS=".$_SESSION['timestamp']."&amp;sortMethod=time\">Time</A></TD></TR>");
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" nowrap><A HREF=\"index.php?ID=0&amp;subMenu=2&amp;action=view&amp;TS=".$_SESSION['timestamp']."&amp;sortMethod=client\">Client Name</A></TD></TR>");
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" style=\"color:#000000;\" nowrap>Spot &raquo;</TD></TR>");
		}else{
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" nowrap><A HREF=\"index.php?ID=0&amp;subMenu=2&amp;action=view&amp;TS=".$_SESSION['timestamp']."&amp;sortMethod=time\">Time</A></TD></TR>");
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" nowrap><A HREF=\"index.php?ID=0&amp;subMenu=2&amp;action=view&amp;TS=".$_SESSION['timestamp']."&amp;sortMethod=client\">Client Name</A></TD></TR>");
			echo("<TR><TD align=\"left\" class=\"switchboardInfo\" nowrap><A HREF=\"index.php?ID=0&amp;subMenu=2&amp;action=view&amp;TS=".$_SESSION['timestamp']."&amp;sortMethod=allSpots\">Spot</A></TD></TR>");
		}

		echo"</TABLE>";
	
		if($sortMethod == 'time'){
			echo"<P align=\"center\"><A HREF=\"pdf/Generate_Live_Grid.php?TS=".$_SESSION['timestamp']."&amp;sortMethod=time\"><IMG src=\"images/Print_Grid.gif\" style=\"border:0px;\" ALT=\"Print This Grid View\"></A></P>";
		}else if($sortMethod == 'client'){
			echo"<P align=\"center\"><A HREF=\"pdf/Generate_Live_Grid.php?TS=".$_SESSION['timestamp']."&amp;sortMethod=client\"><IMG src=\"images/Print_Grid.gif\" style=\"border:0px;\" ALT=\"Print This Grid View\"></A></P>";
		}else if ($sortMethod == 'allSpots'){
			echo"<P align=\"center\"><A HREF=\"pdf/Generate_Live_Grid.php?TS=".$_SESSION['timestamp']."&amp;sortMethod=allSpots\"><IMG src=\"images/Print_Grid.gif\" style=\"border:0px;\" ALT=\"Print This Grid View\"></A></P>";
		}else if ($sortMethod == 'single'){
			echo"<P align=\"center\"><A HREF=\"pdf/Generate_Live_Grid.php?TS=".$_SESSION['timestamp']."&amp;sortMethod=single\"><IMG src=\"images/Print_Grid.gif\" style=\"border:0px;\" ALT=\"Print This Grid View\" style=\"padding-bottom:5px;\"></A></P>";
		}else if ($sortMethod == 'spot'){
			echo"<P align=\"center\"><A HREF=\"pdf/Generate_Live_Grid.php?TS=".$_SESSION['timestamp']."&amp;sortMethod=spot\"><IMG src=\"images/Print_Grid.gif\" style=\"border:0px;\" ALT=\"Print This Grid View\" style=\"padding-bottom:5px;\"></A></P>";
		}
	}
	//###################################################################################
	//END OPTIONS TABLE FOR LIVE GRID
	//###################################################################################	
	
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

	echo "<P></P>";
	
	//###################################################################################
	//BEGIN SORT FUNCTION FOR DAILY LOG - MANAGE PROGRAMS 
	//###################################################################################
	if($ID == '1' && $subMenu == '0' && (!$_GET['action'] || $_GET['action'] == 'listPrograms')){
		echo "<div align='center'>";
		
					//++++++++++++++++++++++++++++++++++++++++	
					//BEGIN sort sort method for List programs		
					if(isset($_GET['sortMethod'])){
						$_SESSION['sortMethod'] = $_GET['sortMethod'];
					}	
					//END sort sort method for List programs		
					//+++++++++++++++++++++++++++++++++++++++
				echo"
				<script language=\"JavaScript\">
				<!--
				function sort(){
					document.sortPrograms.action = 'index.php?ID=1&subMenu=0&action=listPrograms&sort=' + document.sortPrograms.jump.value;
					document.sortPrograms.submit();
				}
				//-->
				</script>";
					
				echo"<form action=\"\" method=\"post\" enctype=\"multipart/form-data\" name=\"sortPrograms\">";
				echo "<TABLE border=\"0\" cellpadding=\"0\" cellspacing=\"1\" width=\"100%\" style=\"background-color:#000000;\">
				<TR><TD align=\"center\" class=\"mainMenuHeaderColor\">Sort By:</TD>	</TR>";
				
					echo("<TR><TD align=\"center\" class=\"switchboardInfo\" nowrap>");

					echo"<select name=\"jump\" size=\"1\" onChange='sort()'>";
					echo"<option value='all' selected>All</option>";
					for ($i = 0; $i < count($ProgramTypes); $i++) { 
						
						if(($_GET['sort'] == $ProgramTypes[($i-1)] || $_SESSION['sort'] == $ProgramTypes[($i-1)]) && ($i&1)){
							echo"<option value='".$ProgramTypes[($i-1)]."' selected>".$ProgramTypes[$i]."</option>";
						}elseif($i&1){
							echo"<option value='".$ProgramTypes[($i-1)]."' >".$ProgramTypes[$i]."</option>";
						}
						
					}
					
					echo("</select></TD></TR>");
				echo"</TABLE></form>";

		echo "</div>";
	}
	//###################################################################################
	//END SORT FUNCTION FOR DAILY LOG - MANAGE PROGRAMS 
	//###################################################################################
	
	
	
	
	//###################################################################################
	//BEGIN PRINT FUNCTION FOR DAILY LOG
	//###################################################################################
	if($ID == '1' && $subMenu == '1'){
		echo "<div align='center'>";
		if($print_output_log == 'rtf'){
			echo "<a href='rtf/generate_log.php'><IMG SRC=\"images/Print_Log.gif\" style=\"border:0px; padding-bottom:5px;\" ALT=\"Print This Grid View\"></a>";
		}else{
			echo "<a href='pdf/generate_log.php'><IMG SRC=\"images/Print_Log.gif\" style=\"border:0px; padding-bottom:5px;\" ALT=\"Print This Grid View\"></a>";
		}
		echo "</div>";
		echo "<div align='center'>";
		echo "<a href='includes/playlist/create_playlist.php'><IMG SRC=\"images/ExportPlaylist.gif\" style=\"border:0px; padding-bottom:5px;\" ALT=\"Export Playlist\"></a>";
		echo "</div>";
	}
	//###################################################################################
	//END PRINT FUNCTION FOR DAILY LOG
	//###################################################################################


	echo "<P></P>";

	//###################################################################################
	//BEGIN SCROLL LAYER FOR DAILY LOG
	//###################################################################################
	/* if($ID == '1' && $subMenu == '1'){
		echo "<div id='topbar'>";
		echo "<input name=\"quickAdd\" type=\"button\" value=\"Quick Add\" onClick=\"return !showPopup('spotInfo', event);\">";
		echo "</div>";
	} */
	//###################################################################################
	//END SCROLL LAYER FOR DAILY LOG
	//###################################################################################



echo "	<!--End Menu Cell-->
	</TD><TD width=\"635\" style=\"vertical-align:top;\" align=\"center\" >
	<!--Begin Content Cell-->";
	
for ($j = 0; $j < count($menuItems); $j++) {
		if($ID == $menuItemsID[$j]){
		include($pageConent[$j]);
		break;
		}
}


echo "<!--End Content Cell-->
	</TD>
  </TR>
</TABLE>
<!--End Content and Menu Table-->	
	</TD>
  </TR>
</TABLE>

</CENTER>";
echo"</BODY>\n
</HTML>";

//this is used to leave message intact when making an entry and there is a conflict
if($_GET['ID'] == '0' && $subMenu == '2' && $action == 'verify'){//count($_SESSION['displayConflicts']) == '0'){
}else{
	unset($_SESSION['message']);
}

?>